package com.xg.java.util;

import javax.servlet.http.HttpSession;

/**
 * 文件下载进度
 */
public class FileDownProgress {
    private static final String F_D_P_KE = "downFileDownProgress_";

    //当前进度
    private double progress;
    //总大小
    private long totalSize;

    public FileDownProgress(long totalSize) {
        this.totalSize = totalSize;
    }

    public void update(int progress, String key, HttpSession session) {
        this.progress += progress;
        key = F_D_P_KE + key;
        session.setAttribute(key, getProgress());
    }

    public double getProgress() {
        return this.progress / this.totalSize * 100;
    }

    public static String getProgressByKey(String key, HttpSession session) {
        key = F_D_P_KE + key;
        Object attribute = session.getAttribute(key);
        if (attribute != null) {
            return attribute.toString();
        }
        return "0.00";
    }


}
